//
//  TearchFengCaiView.m
//  GC_OC
//
//  Created by lbh on 2022/4/5.
//

#import "TearchFengCaiView.h"

@implementation TearchFengCaiView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}
#pragma mark --Priavte
- (void)makeUI{
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(wid(10));
        make.left.mas_equalTo(wid(20));
    }];
    
    [self.bgView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(wid(0));
        make.right.mas_equalTo(-wid(0));
        make.bottom.mas_equalTo(0);
    }];
}
- (void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}
#pragma mark --UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TearchFengCaiViewCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TearchFengCaiViewCollCell" forIndexPath:indexPath];
    NSDictionary *dict = self.dataSource[indexPath.row];
    NSString *media_image = dict[@"teacher_image"];
    NSString *name = dict[@"teacher_name"];
    NSString *teacher_sex = dict[@"teacher_sex"];
    NSString *zLogo = @"logo_nan01";
    if (teacher_sex.intValue == 2) {
        zLogo = @"Logo_nu01";
    }
    media_image = [media_image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:media_image] placeholderImage:[UIImage imageNamed:zLogo]];
    cell.titleLabel.text = name;
//    CAShapeLayer *maskLayer = [UIView createBezierPath:CGRectMake(0, 0, wid(240 - 10), hig(230)) withRoundingCorners: UIRectCornerBottomRight | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerTopLeft withRadid:CGSizeMake(8, 8)];
//    cell.imageView.layer.mask = maskLayer;
//    CAShapeLayer *maskLayer = [UIView createBezierPath:CGRectMake(0, 0, wid(240 - 10), hig(230)) withRoundingCorners: UIRectCornerBottomRight | UIRectCornerBottomLeft withRadid:CGSizeMake(8, 8)];
//    cell.imageView.layer.mask = maskLayer;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(wid(240), hig(300));
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    OnLineTeacherDetailVC *vc = [[OnLineTeacherDetailVC alloc]init];
    NSDictionary *dict = self.dataSource[indexPath.row];
    vc.dataDict = dict;
    vc.medilassID = self.mediClassID;
    vc.hidesBottomBarWhenPushed=YES;
    [[KFTools getCurrentVC].navigationController pushViewController:vc animated:NO];
}
#pragma mark --Lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:FontSize(14)];;
        _titleLabel.textColor = UIColor.blackColor;
        _titleLabel.text = @"教师介绍";
    }
    return _titleLabel;
}
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = wid(10);
        layout.minimumInteritemSpacing = wid(0);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = YES;
        [_collectionView registerClass:[TearchFengCaiViewCollCell class] forCellWithReuseIdentifier:@"TearchFengCaiViewCollCell"];
        _collectionView.userInteractionEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}
@end

@implementation TearchFengCaiViewCollCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}
#pragma mark --Private
- (void)makeUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.imageBgView];
    [self.imageBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
        make.height.mas_equalTo(hig(230));
    }];
    
    [self.imageBgView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageView.mas_bottom).offset(wid(20));
        make.centerX.mas_equalTo(self.bgView.mas_centerX);
    }];
}
#pragma mark --lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor clearColor];
    }
    return _bgView;
}
- (UIView *)imageBgView{
    if (!_imageBgView) {
        _imageBgView = [[UIView alloc]init];
        _imageBgView.backgroundColor = [UIColor clearColor];
        [self addShadowToView:_imageBgView];
    }
    return _imageBgView;
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.cornerRadius = 5;
    }
    return _imageView;
}

/// 添加四边阴影效果
- (void)addShadowToView:(UIView*)shadowView{
   shadowView.layer.masksToBounds = NO;
   shadowView.backgroundColor     = [UIColor greenColor];
   shadowView.layer.shadowColor   = [UIColor.grayColor colorWithAlphaComponent:0.8].CGColor;
   shadowView.layer.shadowOffset  = CGSizeMake(0,1);   //0,0围绕阴影四周  0,4向下有4个像素的偏移
   shadowView.layer.shadowOpacity = 0.5;   //设置阴影透明度
   shadowView.layer.shadowRadius  = 5;      //设置阴影圆角
   shadowView.layer.cornerRadius  = 5;     //设置视图圆角
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:FontSize(14)];
        _titleLabel.textColor = UIColor.blackColor;
        _titleLabel.numberOfLines = UIViewContentModeCenter;
        _titleLabel.text = @"标题";
    }
    return _titleLabel;
}
@end
